package com.yash.rms.controller;

import org.springframework.roo.addon.web.mvc.controller.json.RooWebJson;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.yash.rms.domain.Allocationtype;
import com.yash.rms.domain.Ownership;
import com.yash.rms.domain.Project;
import com.yash.rms.domain.Resource;
import com.yash.rms.domain.ResourceAllocation;

@RequestMapping("/resourceallocations")
@Controller
@RooWebScaffold(path = "resourceallocations", formBackingObject = ResourceAllocation.class)
@RooWebJson(jsonObject = ResourceAllocation.class)
public class ResourceAllocationController {
	
	 	@RequestMapping(produces = "text/html")
	    public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
		 if (page != null || size != null) {
	            int sizeNo = size == null ? 10 : size.intValue();
	            final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
	            uiModel.addAttribute("resources", Resource.findResourceEntries(firstResult, sizeNo));
	            float nrOfPages = (float) Resource.countResources() / sizeNo;
	            uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
	        } else {
	            uiModel.addAttribute("resources", Resource.findAllResources());
	        }
		 
//	        addDateTimeFormatPatterns(uiModel);
	        uiModel.addAttribute("allocationtypes", Allocationtype.findAllAllocationtypes());
	        uiModel.addAttribute("ownerships", Ownership.findAllOwnerships());
	        uiModel.addAttribute("projects", Project.findAllProjects());
	        System.out.println("Inside list method");
	        
	        return "resourceallocations/list";
	    }
	 	 @RequestMapping(value = "/{id}", produces = "text/html")
	     public String show(@PathVariable("employeeId") Integer id, Model uiModel) {
//	         addDateTimeFormatPatterns(uiModel);
	         uiModel.addAttribute("resourceallocation", ResourceAllocation.findResourceAllocation(id));
	         uiModel.addAttribute("itemId", id);
	         return "redirect:/resourceallocations";
	     }
	 	
}
